﻿<!doctype html>
<html>
<head>
    <meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<link rel="shortcut icon" href="favicon.ico">
	<title>Nice Markdown</title>
	<style  type="text/css" >
		html{height: 100%;}
		body{height: 100%; margin:0;padding:0; overflow:hidden;}
	</style>
	<link rel="stylesheet" href="css/editormd.min.css" />
	<script src="/aardio.js"></script>
	<script type="text/javascript" language="javascript">
      window.addEventListener('mousewheel', function(event){
        if (event.ctrlKey || event.metaKey) {
              event.preventDefault();
        }
      },{ passive: false});
//+_
window.onload = function() {
  document.addEventListener('keydown', function (event) {
		if (event.which === 116 || event.which === 114 ||  event.which === 118) {
		  event.preventDefault();
		  return ;
		}

        if ((event.ctrlKey || event.metaKey === true)
            && (event.which === 61 || event.which === 107
            || event.which === 173 || event.which === 109
            || event.which === 187  || event.which === 189))
        {
            event.preventDefault();
        }
    }, false);
}

</script> 
</head>
<body>

<div class="editormd" id="editor">                
	<textarea></textarea>
</div>

<script src="jquery.min.js"></script>
<script src="editormd.min.js"></script>
<script type="text/javascript" language="javascript">
    var host = window.aardio || { 
		openFileDialog: function(){ return Promise.resolve(null)},
		readFile: function(path){ return Promise.resolve(null)},
		writeFile: function(content){ return Promise.resolve(null)},
		openSaveDialog: function(){ return Promise.resolve(null)},
		getSetupFile: function(){ return Promise.resolve(null)},
	};
	var alert = host.alert || window.alert;
</script>

<script type="text/javascript">
//var jQuery = Zepto;
editormd.katexURL  = {
	css : "./lib/KaTeX/katex.min",
	js  : "./lib/KaTeX/katex.min"
};
editormd.homePage = '';
var hasChange = false;
var lastFilePath = '';

const toolbarIcons = ['fileOpen', 'fileSave', 'fileSaveAs', '|',
			"undo", "redo", 
            "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", /*"header", */ 
            "list-ul", "list-ol", "hr", 
            "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "html-entities", "pagebreak",
            "goto-line", "watch", "preview", "clear", "search", 'help',
            '||', 'theme'];

Object.assign(editormd.defaults.lang.toolbar, {
	fileOpen: '打开文件',
	fileSave: '保存文件',
	fileSaveAs: '文件另存为',
	theme: '切换主题',
});

editormd.defaults.lang.dialog.codeBlock.placeholder = '输入代码块';
editormd.defaults.lang.dialog.preformattedText.placeholder = '输入文本或代码块';

var editor = editormd("editor", {
	width  : "100%",
	height : '100%',
	path   : './lib/',
	markdown : '',
	toolbarIcons : function() {
		return toolbarIcons;
	},

	// 指定一个FontAawsome的图标类
	toolbarIconsClass : {
		theme : "fa-moon-o",
		fileOpen : "fa-folder-open",
		fileSave : "fa-floppy-o",
		fileSaveAs : "fa-files-o",
	},

	toolbarCustomIcons: {
		header : '<i class="fa fa-header" onclick=""></i>',
	},
	// 自定义工具栏按钮的事件处理
    toolbarHandlers : {
		theme: function(cm, icon, cursor, selection) {
			if (!this.toolbar){
				return;
			}

			var isDark = icon.hasClass('fa-moon-o');
			setTheme(isDark);
			if(isDark) {
				icon.removeClass('fa-moon-o').addClass('fa-sun-o');
			} else {
				icon.removeClass('fa-sun-o').addClass('fa-moon-o');
			}
		},
		fileOpen: function(cm, icon, cursor, selection) {
			//调用 aardio 函数，所有 aardio 函数都是异步的
			host.openFileDialog().then(function(path){
				if(!path) {
					return ;
				}

				lastFilePath = path;

				host.readFile(path).then(function(v){
				   editor.setMarkdown(v);
				   setTimeout(function(path){ hasChange = false}, 500);
				});
			  }
			);
		},
		fileSave: function(cm, icon, cursor, selection) {
			fileSave();
		},
		fileSaveAs: function(cm, icon, cursor, selection) {
			saveAs();
		},
	},
	disabledKeyMaps : [
       "F11", 'Shift-Ctrl-S'  // disable some default keyboard shortcuts handle
    ],
	placeholder: '开始编写你的Markdown文档',
	syncScrolling : true,
	codeFold : true,
	searchReplace : true,
	saveHTMLToTextarea : true,    // 保存 HTML 到 Textarea
	//watch : false,
	htmlDecode : "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
	emoji : true,
	taskList : true,
	tocm            : true,         // Using [TOCM]
	tex : true,                     // 开启科学公式 TeX 语言支持，默认关闭
	//previewCodeHighlight : false,  // 关闭预览窗口的代码高亮，默认开启
	flowChart : true,  
	sequenceDiagram : true,         // 同上
	onchange: function() {
		hasChange = true;
	},
	onload : function() {
		
		setTimeout(function(){ hasChange = false}, 100);
		var keyMap = {
            "Ctrl-S": function(cm) {
                fileSave();
            },
            "Shift-Ctrl-S": function(cm) {
                saveAs();
            }
        };

        this.addKeyMap(keyMap);

		host.getSetupFile().then(path => {
			if(path) {
				lastFilePath = path;

				host.readFile(path).then(function(v){
				   editor.setMarkdown(v);
				   setTimeout(function(path){ hasChange = false}, 500);
				});
			}
		});

	}
});


function getChange(){
 if(!editor) {
	 return false;
 }
 return hasChange;
}

function fileSave() {
	if(lastFilePath) {
		host.writeFile(lastFilePath, editor.getMarkdown() || '').then(function(v){
			 hasChange = false;
		 });
	} else {
		saveAs();
	}
}

function saveAs() {
	//调用 aardio 函数，所有 aardio 函数都是异步的
	host.openSaveDialog().then(function(path){
		 if(!path) {
		  return;
		 }

		 lastFilePath = path;
		 host.writeFile(path, editor.getMarkdown() || '').then(function(v) {
			 hasChange = false;
		 });
	  }
	);
}

function setTheme(isDark){
	const theme = isDark ?  'dark': 'default';
	editor.setTheme(theme);
	editor.setEditorTheme(isDark ? 'pastel-on-dark': 'default');
	editor.setPreviewTheme(theme);
}
</script>
</body>
</html>
